using System;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace SupremacyEditor
{
    [ValueConversion(typeof(string), typeof(Uri))]
    public class LanguageToFlagImageSourceConverter : IValueConverter
    {
        #region Implementation of IValueConverter
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var languageName = value as string;
            var streamSource = (Stream)null;
            if (languageName != null)
            {
                var cultureInfo = CultureInfo.GetCultureInfo(languageName);
                if (cultureInfo != null)
                {
                    var localeName = cultureInfo.Name;
                    var countryName = (string)null;

                    if (string.Equals(localeName, "en", StringComparison.OrdinalIgnoreCase))
                        localeName = "en-US";

                    var splitIndex = localeName.IndexOf("-");
                    if ((splitIndex >= 0) && (splitIndex < (localeName.Length - 1)))
                    {
                        countryName = localeName.Substring(splitIndex + 1).ToLowerInvariant();
                        localeName = localeName.Substring(0, splitIndex);
                    }

                    if (countryName != null)
                    {
                        var uriSource = new Uri("Resources/Flags/" + countryName + ".png", UriKind.Relative);
                        try
                        {
                            var resourceStream = Application.GetResourceStream(uriSource);
                            if (resourceStream != null)
                                streamSource = resourceStream.Stream;
                        }
                        catch
                        {
                            streamSource = null;
                        }
                    }

                    if (streamSource == null)
                    {
                        var uriSource = new Uri("Resources/Flags/" + localeName + ".png", UriKind.Relative);
                        try
                        {
                            var resourceStream = Application.GetResourceStream(uriSource);
                            if (resourceStream != null)
                                streamSource = resourceStream.Stream;
                        }
                        catch
                        {
                            streamSource = null;
                        }
                    }
                }
            }

            if (streamSource == null)
                return null;

            var result = new BitmapImage();
            
            result.BeginInit();
            result.DecodePixelWidth = 16;
            result.DecodePixelHeight = 11;
            result.StreamSource = streamSource;
            result.EndInit();

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
        #endregion
    }
}